// var tcity = require("../../../utils/citys.js");
var network = require("../../../utils/network.js")
var QQMapWX = require("../../../utils/qqmap-wx-jssdk.js");
var qqmapsdk;
var app = getApp();
Page({
  data:{
    array:['天桥区', '历下区', '历城区', '槐荫区', '市中区', '高新区'],
    name:"请填写您的姓名",
    phone:"请填写您的联系方式",
    condition: false,
    focus:"",
    detail:"街道门牌信息",
  },
    areaPickerBindchange:function(e){
    this.setData({
      areaValue:e.detail.value
    })
  },
  bindPickerChange: function (e) {
    var array = this.data.array 
    var index1 = e.detail.value
    this.setData({
      index: e.detail.value,
      area: array[index1]
    })
  },
  onLoad: function () {
    // 加载腾讯地图服务
    qqmapsdk = new QQMapWX({
      key: 'ILEBZ-GOHRS-EJLOZ-6SNNC-7QSO7-XFBF6'
    })  
  },
  onReady: function () {
    var that = this
    // 加载腾讯地图服务
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        var latitude1 = res.latitude
        var longitude1 = res.longitude
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: latitude1,
            longitude: longitude1
          },
          success: function (res) {
            console.log(res);
            var city1 = res.result.address_component.city
            var district1 = res.result.address_component.district
            that.setData({
              wd: latitude1,
              jd: longitude1,
              city: city1,
              area:district1,
            })
          }
        })     
      }
      })
  }  ,
  formSubmit: function(e) {
    var warn ="";
    var that = this;
    var flag = false;
    if(e.detail.value.name==""){
      warn = "请填写您的姓名！";
    } else if (e.detail.value.phone==""){
      warn = "请填写您的手机号！";
    } else if (!(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value.phone))){
      warn = "手机号格式不正确";
    }else if(e.detail.value.city==''){
      warn = "请选择您的所在城市";
    } else if (e.detail.value.area == '') {
      warn = "请选择您的所在城区";
    } else if (e.detail.value.detail==""){
      warn = "请输入您的具体地址";
    }else{
      flag = true
      console.log('form发生了submit事件，携带数据为：', e.detail.value)
      var newaddress = {}
      newaddress.name = e.detail.value.name
      newaddress.phone = e.detail.value.phone
      newaddress.city = e.detail.value.city
      newaddress.area = e.detail.value.area
      newaddress.detail = e.detail.value.detail
      newaddress.isDefault = true
      newaddress.openid = wx.getStorageSync("openid")
      network.postData(
        "https://api.hsb1234.com/userc/v1/mine/addresses",
        newaddress,
        function (res) { 
          wx.showModal({
            title: '成功',
            content: '保存新地址成功',
          })
        },
        function (res) { }
      )
      wx.setStorageSync('newaddress', newaddress)
      wx.redirectTo({
        url: '../selectAddress/index'      
      }); 
    }
    if(flag==false){
      wx.showModal({
      title: '提示',
      content:warn
    })
    }    
  }  
  })